﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Collections;

namespace Agresso.Interface.Layer.Domain
{
    public class InterfacedBatch //for check history
    {
        public string batch_id = string.Empty;
        public string invoice_number = string.Empty;
        public DateTime pickup_date = DateTime.Today;
        public InterfacedBatch() { }
        public InterfacedBatch(string batch, string invoice, DateTime interfaceDate)
        {
            batch_id = batch;
            invoice_number = invoice;
            pickup_date = interfaceDate;
        }
        public InterfacedBatch(SqlDataReader rd)
        {
            invoice_number = rd["invoice_number"] is DBNull ? "" : rd["invoice_number"].ToString();
            pickup_date = rd["pickup_date"] is DBNull ? new DateTime(2000, 1, 1) : Convert.ToDateTime(rd["pickup_date"]);
            batch_id = rd["batch_id"] is DBNull ? "" : rd["batch_id"].ToString();
        }
    }
    public class CommissionInvoice
    {
        public int ID = 0;
        public string invoice_type = string.Empty;
        public string company = string.Empty;
        public string vessel_name = string.Empty;
        public string vessel_sid = string.Empty;
        public string voyage_no = string.Empty;
        public string profit_centre = string.Empty;
        public DateTime invoice_date = DateTime.Today;
        public string charge_description = string.Empty;
        public string invoice_number = string.Empty;
        public decimal invoice_amount = 0;
        public string invoice_currency = string.Empty;
        public DateTime invoice_due_date = DateTime.Today;
        public string charge_type = string.Empty;
        public string raised_to_final_by = string.Empty;
        public int paid = 0;//--
        public DateTime paid_date = DateTime.Today;//--
        public decimal charge_addr_comm = 0;//--
        public decimal charge_amount = 0;
        public decimal charge_collect_comm = 0;//--
        public decimal charge_non_collect_comm = 0;//--
        public string voyage_type = string.Empty;//--
        public decimal collect_comm = 0;//--
        public decimal collect_comm2 = 0;//--
        public decimal collect_comm3 = 0;//--
        public decimal collect_comm4 = 0;//--
        public int display_order = 0;//--
        public long charge_id = 0;//--
        public int narrative_line = 0;//--
        public string narrative = string.Empty;

        public string pickup_flag = string.Empty;
        public DateTime pickup_date = new DateTime(1900, 1, 1);
        public DateTime pickup_update_date = new DateTime(1900, 1, 1);
        public DateTime insert_date = new DateTime(1900, 1, 1);
        public string remarks = string.Empty;
        public string batch_id = string.Empty;
        public string lloyds_no = string.Empty;

        public int commission_invoice_id = 0;
        public IList Items = new ArrayList();//for GL
        public CommissionInvoice Header = null; //for AP

        public CommissionInvoice() { }
        public CommissionInvoice(SqlDataReader rd, bool isfrominterface)
        {
            ID = rd["ID"] is DBNull ? 0 : Convert.ToInt32(rd["ID"]);
            invoice_type = rd["invoice_type"] is DBNull ? "" : rd["invoice_type"].ToString();
            company = rd["company"] is DBNull ? "" : rd["company"].ToString();
            vessel_name = rd["vessel_name"] is DBNull ? "" : rd["vessel_name"].ToString();
            vessel_sid = rd["vessel_code"] is DBNull ? "" : rd["vessel_code"].ToString();
            voyage_no = rd["voyage_no"] is DBNull ? "" : rd["voyage_no"].ToString();
            profit_centre = rd["profit_centre"] is DBNull ? "" : rd["profit_centre"].ToString();
            invoice_date = rd["invoice_date"] is DBNull ? new DateTime(2000, 1, 1) : Convert.ToDateTime(rd["invoice_date"]);
            charge_description = rd["charge_description"] is DBNull ? "" : rd["charge_description"].ToString();
            invoice_number = rd["invoice_number"] is DBNull ? "" : rd["invoice_number"].ToString();
            invoice_amount = rd["invoice_amount"] is DBNull ? 0 : Convert.ToDecimal(rd["invoice_amount"]);
            invoice_currency = rd["invoice_currency"] is DBNull ? "" : rd["invoice_currency"].ToString();
            invoice_due_date = rd["invoice_due_date"] is DBNull ? new DateTime(2000, 1, 1) : Convert.ToDateTime(rd["invoice_due_date"]);
            charge_type = rd["charge_type"] is DBNull ? "" : rd["charge_type"].ToString();
            raised_to_final_by = rd["raised_to_final_by"] is DBNull ? "" : rd["raised_to_final_by"].ToString();
            paid = rd["paid"] is DBNull ? 0 : Convert.ToInt16(rd["paid"]);
            paid_date = rd["paid_date"] is DBNull ? new DateTime(2000, 1, 1) : Convert.ToDateTime(rd["paid_date"]);
            charge_amount = rd["charge_amount"] is DBNull ? 0 : Convert.ToDecimal(rd["charge_amount"]);
            charge_addr_comm = rd["charge_addr_comm"] is DBNull ? 0 : Convert.ToDecimal(rd["charge_addr_comm"]);
            charge_collect_comm = rd["charge_collect_comm"] is DBNull ? 0 : Convert.ToDecimal(rd["charge_collect_comm"]);
            charge_non_collect_comm = rd["charge_non_collect_comm"] is DBNull ? 0 : Convert.ToDecimal(rd["charge_non_collect_comm"]);
            voyage_type = rd["voyage_type"] is DBNull ? "" : rd["voyage_type"].ToString();
            collect_comm = rd["collect_comm"] is DBNull ? 0 : Convert.ToDecimal(rd["collect_comm"]);
            collect_comm2 = rd["collect_comm2"] is DBNull ? 0 : Convert.ToDecimal(rd["collect_comm2"]);
            collect_comm3 = rd["collect_comm3"] is DBNull ? 0 : Convert.ToDecimal(rd["collect_comm3"]);
            collect_comm4 = rd["collect_comm4"] is DBNull ? 0 : Convert.ToDecimal(rd["collect_comm4"]);
            display_order = rd["display_order"] is DBNull ? 0 : Convert.ToInt16(rd["display_order"]);
            charge_id = rd["charge_id"] is DBNull ? 0 : Convert.ToInt64(rd["charge_id"]);
            narrative_line = rd["narrative_line"] is DBNull ? 0 : Convert.ToInt16(rd["narrative_line"]);
            narrative = rd["narrative"] is DBNull ? "" : rd["narrative"].ToString();


            if (isfrominterface)
            {
                pickup_flag = rd["pickup_flag"] is DBNull ? "" : rd["pickup_flag"].ToString();
                pickup_date = rd["pickup_date"] is DBNull ? new DateTime(2000, 1, 1) : Convert.ToDateTime(rd["pickup_date"]);
                pickup_update_date = rd["pickup_update_date"] is DBNull ? new DateTime(2000, 1, 1) : Convert.ToDateTime(rd["pickup_update_date"]);
                insert_date = rd["insert_date"] is DBNull ? new DateTime(2000, 1, 1) : Convert.ToDateTime(rd["insert_date"]);
                commission_invoice_id = rd["commission_invoice_id"] is DBNull ? 0 : Convert.ToInt32(rd["commission_invoice_id"]);
                remarks = rd["remarks"] is DBNull ? "" : rd["remarks"].ToString();
                batch_id = rd["batch_id"] is DBNull ? "" : rd["batch_id"].ToString();
            }
        }
        /// <summary>
        /// For Alert
        /// </summary>
        /// <param name="rd"></param>
        public CommissionInvoice(SqlDataReader rd)
        {
            invoice_number = rd["invoice_number"] is DBNull ? "" : rd["invoice_number"].ToString();
            pickup_date = rd["pickup_date"] is DBNull ? new DateTime(2000, 1, 1) : Convert.ToDateTime(rd["pickup_date"]);
            remarks = rd["remarks"] is DBNull ? "" : rd["remarks"].ToString();
            invoice_date = rd["invoice_date"] is DBNull ? new DateTime(2000, 1, 1) : Convert.ToDateTime(rd["invoice_date"]);
        }
        public CommissionInvoice(SqlDataReader rd, string proforma)
        {
            company = rd["company"] is DBNull ? "" : rd["company"].ToString();
            vessel_name = rd["vessel_name"] is DBNull ? "" : rd["vessel_name"].ToString();
            voyage_no = rd["voyage_no"] is DBNull ? "" : rd["voyage_no"].ToString();
            invoice_date = rd["invoice_date"] is DBNull ? new DateTime(2000, 1, 1) : Convert.ToDateTime(rd["invoice_date"]);
            invoice_amount = rd["invoice_amount"] is DBNull ? 0 : Convert.ToDecimal(rd["invoice_amount"]);
            invoice_type = rd["invoice_type"] is DBNull ? "" : rd["invoice_type"].ToString();

            invoice_number = rd["invoice_number"] is DBNull ? "" : rd["invoice_number"].ToString();
            pickup_date = rd["pickup_date"] is DBNull ? new DateTime(2000, 1, 1) : Convert.ToDateTime(rd["pickup_date"]);
            remarks = rd["remarks"] is DBNull ? "" : rd["remarks"].ToString();
            profit_centre = rd["profit_centre"] is DBNull ? "" : rd["profit_centre"].ToString();
        }

        public bool IsInterco(acrbatchinput acr)
        {
            bool result = false;
            switch (company)
            {
                case "PCL Shipping":
                    result = true;
                    acr.dim_1 = "D6103";
                    acr.dim_6 = "610";
                    break;
                case "Pacific Ship-Manager SB":
                    result = true;
                    acr.dim_1 = "D5012";
                    acr.dim_6 = "501";
                    break;
                case "PCL Tanker":
                    result = true;
                    acr.dim_1 = "D6022";
                    acr.dim_6 = "602";
                    break;
            }
            return result;
        }
        public bool collectCommissionOneTCRelet()
        {
            decimal totalcommission = 0;
            decimal totalcommission2 = 0;
            foreach (CommissionInvoice ci in Items)
            {
                decimal collectcomm = ci.collect_comm < 0 ? -ci.collect_comm : ci.collect_comm;
                decimal collectcomm2 = ci.collect_comm2 < 0 ? -ci.collect_comm2 : ci.collect_comm2;
                totalcommission += collectcomm;
                totalcommission2 += collectcomm2;
            }
            decimal invoiceAmn = invoice_amount < 0 ? -invoice_amount : invoice_amount;
            totalcommission = totalcommission - invoiceAmn;
            totalcommission = totalcommission < 0 ? -totalcommission : totalcommission;
            totalcommission2 = totalcommission2 - invoiceAmn;
            totalcommission2 = totalcommission2 < 0 ? -totalcommission2 : totalcommission2;
            if (totalcommission < 1)
                return true;
            if (totalcommission2 < 1)
                return false;
            return true;
        }
        public decimal GetCommissionCreditTCorVCReletAmountRevision1()
        {
            decimal result = 0;
            if (Header.Items.Count == 1)
                return invoice_amount;
            if (invoice_amount - GetTotalCollectCommission(1) == 0)
            {
                return collect_comm;
            }
            if (invoice_amount - GetTotalCollectCommission(2) == 0)
            {
                return collect_comm2;
            }
            if (invoice_amount - GetTotalCollectCommission(3) == 0)
            {
                return collect_comm3;
            }
            return result;
        }
        public decimal GetTotalCollectCommission(int i)
        {
            decimal collectAmount = 0;
            switch (i)
            {
                case 1:
                    foreach (CommissionInvoice j in Header.Items)
                    {
                        collectAmount += j.collect_comm;
                    } break;
                case 2:
                    foreach (CommissionInvoice j in Header.Items)
                    {
                        collectAmount += j.collect_comm2;
                    } break;
                case 3:
                    foreach (CommissionInvoice j in Header.Items)
                    {
                        collectAmount += j.collect_comm3;
                    } break;
            }
            return collectAmount;
        }
        public decimal GetCommissionCreditTCorVCReletAmount()
        {
            decimal result = 0;
            if (Header.voyage_type.Trim() == "TC Relet")
            {
                return GetCommissionAmountTCReletc();
            }
            if (Header.voyage_type.Trim() == "Cargo Relet" || Header.voyage_type.Trim() == "Voyage")
            {
                return GetCommissionAmountVCRelet();
            }
            return result;
        }
        public decimal GetCommissionAmountTCReletc()
        {
            decimal result = 0;
            if (Header.Items.Count == 1)
            {
                return invoice_amount;
            }
            if (this.collect_comm == 0)
            {
                return charge_collect_comm;
            }
            else
            {
                decimal totalcommission1 = 0;
                decimal totalcommission2 = 0;
                foreach (CommissionInvoice item in Header.Items)
                {
                    decimal collectcomm = item.collect_comm == 0 ? item.charge_collect_comm : item.collect_comm;//< 0 ? -item.collect_comm : item.collect_comm;
                    decimal collectcomm2 = item.collect_comm2;// < 0 ? -item.collect_comm2 : item.collect_comm2;
                    totalcommission1 += collectcomm;
                    totalcommission2 += collectcomm2;
                }
                decimal invoiceAmn = invoice_amount < 0 ? -invoice_amount : invoice_amount;
                totalcommission1 = totalcommission1 < 0 ? -totalcommission1 : totalcommission1;
                totalcommission2 = totalcommission2 < 0 ? -totalcommission2 : totalcommission2;

                if (totalcommission1 - invoiceAmn == 0)
                    return collect_comm;
                if (totalcommission2 - invoiceAmn == 0)
                    return collect_comm2;
            }
            return result;
        }
        public decimal GetCommissionAmountVCRelet()
        {
            decimal result =0;
            if (Header == null) return 0;
            if (Header.Items.Count == 1) return this.invoice_amount;
            decimal charge_collect_amount = 0;
            decimal charge_addr_amount = 0;
            foreach (CommissionInvoice item in Header.Items)
            {
                charge_collect_amount += item.charge_collect_comm;
                charge_addr_amount += item.charge_addr_comm;
            }
            if (this.invoice_amount - charge_collect_amount == 0)
                return this.charge_collect_comm;
            if (this.invoice_amount - charge_addr_amount == 0)
                return this.charge_addr_comm;
            return result;
        }
        public decimal GetTotalCollectCommission()
        {
            decimal total = 0;
            foreach (CommissionInvoice item in Header.Items)
            {
                total += item.charge_collect_comm;
            }
            return total;
        }
        public decimal GetTotalCollectCommission1()
        {
            decimal total = 0;
            foreach (CommissionInvoice item in Header.Items)
            {
                total += item.collect_comm;
            }
            return total;
        }
        public decimal GetTotalCollectCommission2()
        {
            decimal total = 0;
            foreach (CommissionInvoice item in Header.Items)
            {
                total += item.collect_comm2;
            }
            return total;
        }
        public decimal GetTotalCollectCommission3()
        {
            decimal total = 0;
            foreach (CommissionInvoice item in Header.Items)
            {
                total += item.collect_comm3;
            }
            return total;
        }
        public decimal GetTotalCollectCommission4()
        {
            decimal total = 0;
            foreach (CommissionInvoice item in Header.Items)
            {
                total += item.collect_comm4;
            }
            return total;
        }
        public decimal GetTotalChargeAddressCommission()
        {
            decimal total = 0;
            foreach (CommissionInvoice item in Header.Items)
            {
                total += item.charge_addr_comm;
            }
            return total;
        }

        public decimal GetCollectCommissionAmount(int comm)
        {
            switch (comm)
            {
                case 0 :
                    return charge_collect_comm;
                case 1:
                    return collect_comm;
                case 2:
                    return collect_comm2;
                case 3:
                    return collect_comm3;
                case 4:
                    return collect_comm4;
                case 5:
                    return charge_addr_comm;
                default:
                    return charge_collect_comm;
            }
        }

        public DateTime GetInvoiceDate()
        {
            return new DateTime(invoice_date.Year, invoice_date.Month, invoice_date.Day);
        }
    }
}
